package com.markus.code.剑指offer;

/**
 * Author:markusZhang
 * Date:Create in 2020/8/31 13:23
 * todo: 表示数值的字符串
 */
public class IsNumber {
    private int index = 0;
    public boolean isNumber(String s) {
        if (s == null || s.length() == 0){
            return false;
        }
        char[] nums = s.trim().toCharArray();
        boolean A = scanInteger(nums),B = false,C = false;
        if (index < nums.length && nums[index] == '.'){
            index ++ ;
            B = scanUnsignedInteger(nums);
        }
        if (index < nums.length && (nums[index] == 'e' || nums[index] == 'E')){
            index ++ ;
            C = scanInteger(nums);
            if (C == false){
                return false;
            }
        }
        return index == nums.length && (A||B);
    }
    private boolean scanInteger(char []nums){
        if (index < nums.length && (nums[index] == '+' || nums[index] == '-')){
            index++;
        }
        return scanUnsignedInteger(nums);
    }
    private boolean scanUnsignedInteger(char []nums){
        int start = index;
        while(index < nums.length && nums[index] <= '9' && nums[index] >= '0'){
            index++;
        }
        return index>start;
    }
    public static void main(String[] args) {
        String s = "e";
        IsNumber demo = new IsNumber();
        System.out.println(demo.isNumber(s));
    }
}
